﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using UDP_Lib;

namespace FastUDPsend
{
	/// <summary>
	/// Interaction logic for UDP_Control.xaml
	/// </summary>
    public partial class UDP_Control : INotifyPropertyChanged
	{
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        private void OnPropertyChanged(string propertyName)
        {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        private string _serverIp="224.0.0.1";
        public string ServerIP
        {
            get { return _serverIp; }
            set
            {
                _serverIp = value;
                if (CurClient!=null)
                    CurClient.ServerIP = value;
                if (CurServer != null)
                    CurServer.ServerIP = value;
                OnPropertyChanged("ServerIP");
            }
        }

	    private bool _joinMulti;
	    public bool JoinMulti
	    {
	        get { return _joinMulti; }
	        set
	        {
	            _joinMulti = value;
                if(_curClient!=null)
                    _curClient.JoinMulti = JoinMulti;

                if (_curServer != null)
                    _curServer.JoinMulti = JoinMulti;


                OnPropertyChanged("JoinMulti");
	        }
	    }

	    private int _serverPort=5000;
        public int ServerPort
        {
            get { return _serverPort; }
            set
            {
                _serverPort = value;
                if (CurClient != null)
                    CurClient.ServerPort = value;
                if (CurServer != null)
                    CurServer.ServerPort = value;
                OnPropertyChanged("ServerPort");
            }
        }

	    private UDP_Client _curClient;
	    public UDP_Client CurClient
	    {
	        get { return _curClient; }
	        set
	        {
	            _curClient = value;
                if (_curClient != null)
                {
                    _curClient.ServerPort = ServerPort;
                    _curClient.ServerIP = ServerIP;
                    _curClient.JoinMulti = JoinMulti;
                }
                OnPropertyChanged("cur_client");
	        }
	    }

	    private UDP_Server _curServer;
	    public UDP_Server CurServer
	    {
	        get { return _curServer; }
	        set
	        {
	            _curServer = value;
                if (_curServer != null)
                {
                    _curServer.ServerPort = ServerPort;
                    _curServer.ServerIP = ServerIP;
                    _curServer.JoinMulti = JoinMulti;
                }
                OnPropertyChanged("cur_server");
	        }
	    }

	    DispatcherTimer dt =new DispatcherTimer();

		public UDP_Control()
		{
			this.InitializeComponent();
            DataContext = this;

            CurClient = new UDP_Client();
            CurServer = new UDP_Server();		    


            this.Loaded += new RoutedEventHandler(UDP_Control_Loaded);
		}

        void UDP_Control_Loaded(object sender, RoutedEventArgs e)
        {
            //throw new NotImplementedException();
            dt.Interval = TimeSpan.FromMilliseconds(10);
            dt.Tick += dt_Tick;
        }

        void dt_Tick(object sender, EventArgs e)
        {
            this.Dispatcher.BeginInvoke(
                DispatcherPriority.Send,
                (ThreadStart)delegate()
                {
                    var bufSend = new byte[Convert.ToInt32(tb_SizeData1.Text)];
                    bufSend[0] = 42;
                    CurClient.SendBigBuffer(bufSend);
                });
            //  throw new NotImplementedException();
        }

        private void btnStartSender_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if(((ToggleButton) sender).IsChecked==true)
            {
                dt.Start();
                CurClient.Start();
            }
            else
            {
                dt.Stop();
                CurClient.Stop();
            }

        	// TODO: Add event handler implementation here.
        }

        private void btn_Listen_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (((ToggleButton)sender).IsChecked == true)
            {
                CurServer.evRecBuf += (cur_server_evRecBuf);
                CurServer.Start();
            }
            else
            {
                CurServer.Stop();
                CurServer.evRecBuf -= (cur_server_evRecBuf);
            }
        	// TODO: Add event handler implementation here.
        }

	    private void cur_server_evRecBuf(byte[] bbyf)
	    {
	        //throw new NotImplementedException();
	    }
	}
}